<script type="text/javascript">
	$(document).ready(function() {
		$('input:checkbox').not('th#select_all input:checkbox').click(function(){
			countChecked();
		});
		
		countChecked();	
		$('div.media').hover(function(){
			$(this).find('div.menu-hover').show();
		},function(){
			$(this).find('div.menu-hover').hide();
		});
		$('#addModal').on('hidden', function() {
			$(this).data('modal').$element.removeData();
		});
		$('#editModal').on('hidden', function() {
			$(this).data('modal').$element.removeData();
		});
		$('#deleteModal').on('hidden', function() {
			$(this).data('modal').$element.removeData();
		});
		$('#achieveModal').on('hidden', function() {
			$(this).data('modal').$element.removeData();
		});
	});
	function countChecked(){
		count_checked = $('input:checked').not('#select_all input:checkbox').length;
		if(count_checked >= 1){
			$('#mass-action').show();
		}else{
			$('#select_all input:checkbox').removeAttr('checked');
			$('#mass-action').hide();
		}
	}
</script>
<style>
.modal-body {
    max-height: 700px;
}
.label{
    font-size: 11px;
    font-weight: normal;
}
.media:hover {
	background-color:#FFFFD6;
}
</style>
<div class="container">	
	<div class="page-header">
		<h2>
			Documents
		</h2>	  
	</div>
	<?php echo $this->Session->flash('flash', array('element' => $this->Session->read('status')));?>
	<?php
		if($_SESSION['UserAuth']['User']['user_group_id'] == 1){
	?>
	<div class="btn-toolbar pull-left" style="margin: 0;display:none;" id='mass-action'>
		<div class="btn-group">
			<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
				Action
				<span class="caret"></span>
			</a>
			<ul class="dropdown-menu">
			  <li><a href="/documents/deleteSelectedConfirm" id='deleteSelectedConfirm'>Delete</a></li>
			  <li><a href="/documents/archiveSelectedConfirm" id='archiveSelectedConfirm'>Archive</a></li>
			</ul>
		</div>
	</div>
	<div class="btn-toolbar pull-right" style="margin: 0;">
		<div class="btn-group">
			<a data-remote="/documents/add" role="button" class="btn btn-danger" id='add' data-toggle="modal" data-target="#addModal" data-cache='false'>New Document</a>
		</div>
	</div><br /><br />
	<?php
		}
	?>
	<div>
	<?php
		foreach($documents as $document){
			$links = unserialize($document['Document']['link']);
			$img = '';
			$url_alternate = '';
			foreach($links as $link){
				if($link['@type'] == 'image/png'){
					$img = $link['@href'];
				}else if($link['@rel'] == 'alternate'){
					$url_alternate = $link['@href'];
				}
			}
	?>
		<div class="media table-hover">
		<?php
			if($_SESSION['UserAuth']['User']['user_group_id'] == 1){
		?>
		  <input type="checkbox" class="pull-left" value='<?php echo $document['Document']['id'];?>'>
		<?php
			}
		?>
		  <a href="<?php echo $url_alternate;?>" class="pull-left">
			<img src="<?php echo $img;?>" width='64' class="media-object img-polaroid">
		  </a>
		  <div class="media-body">
			<h4 class="media-heading">
				<a href='<?php echo $url_alternate;?>' target='_blank'><?php echo $document['Document']['name'];?></a>
				<small>
					<?php
						if($_SESSION['UserAuth']['User']['user_group_id'] == 1){
					?>
					<div class="pull-right span1">
						<div class="dropdown menu-hover pull-right" style='display:none;z-index:100;'>
							<a class="dropdown-toggle icon-chevron-down pull-right" data-toggle="dropdown" href="#" data-placement="right"></a>
							<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
								<li><a data-remote="/documents/edit/<?php echo $document['Document']['id'];?>" role="button" class="" data-toggle="modal" data-target="#editModal" data-cache='false'>Edit</a></li>
								<?php
									if($document['Document']['deleted'] == 1){
								?>
									<li><a data-remote="/documents/purgeConfirm/<?php echo $document['Document']['id'];?>" role="button" class="" data-toggle="modal" data-target="#deleteModal" data-cache='false'>Purge</a></li>
								<?php
									}else{
								?>
									<li><a data-remote="/documents/deleteConfirm/<?php echo $document['Document']['id'];?>" role="button" class="" data-toggle="modal" data-target="#deleteModal" data-cache='false'>Delete</a></li>
								<?php
									}
								?>
								<li><a data-remote="/documents/archive/<?php echo $document['Document']['id'];?>" role="button" class="" data-toggle="modal" data-target="#deleteModal" data-cache='false'>Archive</a></li>
							</ul>
						</div>
					</div>
					<?php
						}
					?>
				</small>
			</h4>
			<p>
			<?php echo $document['Document']['note'];?>
			</p>
			<small>
				<span class="label label-info">Owner</span> <?php echo $document['Document']['owner'];?>  
				<span class="label label-info">Release</span> <?php echo date('F jS Y',strtotime($document['Document']['release_date']));?> 
				<span class="label label-info">Serial</span> <?php echo $document['Document']['serial'];?> 
			<?php 
				if(!empty($document['Document']['archive_date'])){
			?>
			<span class="label label-info">Expire</span> <?php echo date('F jS Y',strtotime($document['Document']['archive_date']));?>
			<?php
				}
				if($document['Document']['deleted'] == 1){
			?>
			<span class="label label-info">Deleted</span> <?php echo date('F jS Y',strtotime($document['Document']['deleted_date']));?>
			<?php
				}
			?>
			</small>
		  </div>
		</div>
		<hr />
	<?php
		}
	?>
		<div class="pagination pagination-centered">
		<ul>
		<?php
			echo $this->Paginator->prev(__('previous'), array('tag' => 'li'), null, array('class' => 'prev disabled','tag' => 'li'));
			echo $this->Paginator->numbers(array('tag' => 'li','separator'=>'','currentClass'=>'active'));
			echo $this->Paginator->next(__('next'), array('tag' => 'li'), null, array('class' => 'next disabled','tag' => 'li'));
		?>
		</ul>
		</div>
	</div>
</div>
<div id="addModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true" height='700'>
	<div class="modal-body"></div>
</div>
<div id="editModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
	<div class="modal-body"></div>
</div>
<div id="deleteModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
	<div class="modal-body"></div>
</div>
<div id="achieveModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="achieveModalLabel" aria-hidden="true">
	<div class="modal-body"></div>
</div>
